package com.wdh.kjs.activity;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.wdh.kjs.R;
import com.wdh.kjs.http.Httpurl;
import com.wdh.kjs.model.JsonModel_my;
import com.wdh.kjs.util.ToastUtils;

import java.math.BigDecimal;

/**
 * Created by CQH on 2016/4/21.
 */
public class AccountOverviewActivity extends BaseTopActivity implements View.OnClickListener{
    private TextView totalAssert;
    private TextView availableBalance;
    private TextView blockedBalance;
    private TextView totalBondInvestBack;
    private TextView totalBondInvestIncome;
    private TextView bondInvest;
    private TextView totalBondInvestIncomeReceivable;
    private TextView totalReceivable;
    private TextView totalBondRaiseRepay;
    private TextView totalBondRaiseInterestRepay;
    private TextView totalBondRaisePrincipleStill;
    private TextView totalBondRaiseInterestStill;
    private TextView totalStill;
    private JsonModel_my jsonModel_my;
    private String partyId;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_account_overview);
        SharedPreferences share = getSharedPreferences("logininfo", MODE_PRIVATE);
        partyId = share.getString("loginToken", "");
        initTopBar("账户总览");
        initview();
        loadData();

    }

    private void initview() {
        totalAssert = (TextView)findViewById(R.id.totalAssert);                        //总资产
        availableBalance = (TextView)findViewById(R.id.availableBalance);             //可用余额
        blockedBalance = (TextView)findViewById(R.id.blockedBalance);                  //冻结金额

        totalBondInvestBack = (TextView)findViewById(R.id.totalBondInvestBack);       //累计投资收回
        totalBondInvestIncome = (TextView)findViewById(R.id.totalBondInvestIncome);  //累计已收收益
        bondInvest = (TextView)findViewById(R.id.bondInvest);                          //待收本金
        totalBondInvestIncomeReceivable = (TextView)findViewById(R.id.totalBondInvestIncomeReceivable);  //待收收益
        totalReceivable = (TextView)findViewById(R.id.totalReceivable);                                     //待收总额

        totalBondRaiseRepay = (TextView)findViewById(R.id.totalBondRaiseRepay);                            //累计融资返还
        totalBondRaiseInterestRepay = (TextView)findViewById(R.id.totalBondRaiseInterestRepay);          //累计已还收益
        totalBondRaisePrincipleStill = (TextView)findViewById(R.id.totalBondRaisePrincipleStill);        //待还本金
        totalBondRaiseInterestStill = (TextView)findViewById(R.id.totalBondRaiseInterestStill);          //待还收益
        totalStill = (TextView)findViewById(R.id.totalStill);                                                //待还总额
    }

    private void loadData() {
        RequestParams params = new RequestParams();
        params.addBodyParameter("partyId", partyId);

        HttpUtils http = new HttpUtils();
        http.send(HttpRequest.HttpMethod.POST, Httpurl.URL_MY_ACCOUNT, params, new RequestCallBack<String>() {

            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {

                Gson gson = new Gson();
                jsonModel_my = gson.fromJson(responseInfo.result, JsonModel_my.class);

                if (jsonModel_my.success) {
                    totalAssert.setText("￥"+ new BigDecimal(jsonModel_my.getUserVirtualAccount().getTotalAssets().toString()).setScale(2)+"元");
                    availableBalance.setText("￥"+ new BigDecimal(jsonModel_my.getUserVirtualAccount().getAvailableBalance().toString()).setScale(2)+"元");
                    blockedBalance.setText("￥"+ new BigDecimal(jsonModel_my.getUserVirtualAccount().getBlockedBalance().toString()).setScale(2)+"元");

                    totalBondInvestBack.setText("￥"+ new BigDecimal(jsonModel_my.getUserVirtualAccount().getTotalBondInvestBack().toString()).setScale(2)+"元");
                    totalBondInvestIncome.setText("￥"+ new BigDecimal(jsonModel_my.getUserVirtualAccount().getTotalBondInvestIncome().toString()).setScale(2)+"元");
                    bondInvest.setText("￥"+ new BigDecimal(jsonModel_my.getUserVirtualAccount().getBondInvest().toString()).setScale(2)+"元");
                    totalBondInvestIncomeReceivable.setText("￥"+ new BigDecimal(jsonModel_my.getUserVirtualAccount().getTotalBondInvestIncomeReceivable().toString()).setScale(2)+"元");
                    totalReceivable.setText("￥"+ new BigDecimal(jsonModel_my.getTotalReceivable().toString()).setScale(2)+"元");

                    totalBondRaiseRepay.setText("￥"+ new BigDecimal(jsonModel_my.getUserVirtualAccount().getTotalBondRaiseRepay().toString()).setScale(2)+"元");
                    totalBondRaiseInterestRepay.setText("￥"+ new BigDecimal(jsonModel_my.getUserVirtualAccount().getTotalBondRaiseInterestRepay().toString()).setScale(2)+"元");
                    totalBondRaisePrincipleStill.setText("￥"+ new BigDecimal(jsonModel_my.getUserVirtualAccount().getTotalBondRaisePrincipleStill().toString()).setScale(2)+"元");
                    totalBondRaiseInterestStill.setText("￥"+ new BigDecimal(jsonModel_my.getUserVirtualAccount().getTotalBondRaiseInterestStill().toString()).setScale(2)+"元");
                    totalStill.setText("￥"+ new BigDecimal(jsonModel_my.getTotalStill().toString()).setScale(2)+"元");
                }
            }

            @Override
            public void onFailure(HttpException error, String msg) {
                ToastUtils.showShort(getApplicationContext(), "网络异常！");
            }
        });

    }
    @Override
    public void onClick(View view) {
    }
}
